package com.ytjj.qmyx.data.amqp.consumer;

import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import com.ytjj.common.constants.QueueConstants;
import com.ytjj.qmyx.data.mapper.OrderProblemMapper;
import com.ytjj.qmyx.data.model.request.OrderProblemReplyRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Component
public class OrderProblemListener {

    @Resource
    private OrderProblemMapper orderProblemMapper;

    @RabbitListener(queuesToDeclare = @Queue(value = QueueConstants.ORDER_PROBLEM_REPLY, durable = "true"))
    public void selectAndAddProduces(String orderProblemReplyStr, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag, Channel channel) {
        OrderProblemReplyRequest orderProblemReplyRequest = JSONObject.parseObject(orderProblemReplyStr, OrderProblemReplyRequest.class);
        if (null != orderProblemReplyRequest) {
            if (orderProblemReplyRequest.getReplyRole() == 1) {
                List<String> adminList = orderProblemMapper.getAllRoleAdmin("DOCUMENTARY");
                if (!CollectionUtils.isEmpty(adminList)) {
                    for (String admin : adminList) {
                        orderProblemReplyRequest.setReceiveUserName(admin);
                        Integer receiveUser = orderProblemMapper.getAdminIdByName(orderProblemReplyRequest.getReceiveUserName());
                        orderProblemReplyRequest.setReceiveUser(receiveUser);
                        orderProblemMapper.insert(orderProblemReplyRequest);
                    }
                }

            } else if (orderProblemReplyRequest.getReplyRole() == 2) {
                Integer receiveUser = orderProblemMapper.getAdminIdByName(orderProblemReplyRequest.getReceiveUserName());
                orderProblemReplyRequest.setReceiveUser(receiveUser);
                orderProblemMapper.insert(orderProblemReplyRequest);
            } else if (orderProblemReplyRequest.getReplyRole() == 3) {
                Integer receiveUser = orderProblemMapper.getAdminIdByName(orderProblemReplyRequest.getReceiveUserName());
                orderProblemReplyRequest.setReceiveUser(receiveUser);
                orderProblemMapper.insert(orderProblemReplyRequest);
                List<String> adminList = orderProblemMapper.getAllRoleAdmin("DOCUMENTARY");
                if (!CollectionUtils.isEmpty(adminList)) {
                    for (String admin : adminList) {
                        orderProblemReplyRequest.setReceiveUserName(admin);
                        Integer user = orderProblemMapper.getAdminIdByName(orderProblemReplyRequest.getReceiveUserName());
                        orderProblemReplyRequest.setReceiveUser(user);
                        orderProblemMapper.insert(orderProblemReplyRequest);
                    }
                }
            }
        }
    }
}